﻿namespace KeRui.Upload
{
    using Microsoft.Win32;
    using System;
    using System.Globalization;
    using System.Reflection;
    using System.Text;
    using System.Web;

    public class Registration
    {
        private static bool sChecked;
        private static bool sLite;
        private static string sName;
        private static string sPath;
        private static string sVers;

        internal static void CheckValid()
        {
            try
            {
                if (!(IsValid(SerialKey) || (GetTimeLeft().TotalDays > 0.0)))
                {
                    throw new Exception("Your license has expired.");
                }
            }
            catch
            {
            }
        }

        private static string Decode(string inString)
        {
            return EncodeDecode(inString);
        }

        private static string Encode(string inString)
        {
            return EncodeDecode(inString);
        }

        private static string EncodeDecode(string inString)
        {
            if (inString == null)
            {
                return "";
            }
            char[] chArray = inString.ToCharArray();
            for (int i = 0; i < chArray.Length; i++)
            {
                chArray[i] = (char) (chArray[i] ^ ((char) (0x17fd + i)));
            }
            StringBuilder builder = new StringBuilder(chArray.Length);
            builder.Append(chArray);
            return builder.ToString();
        }

        private static DateTime GetInstallDate(string inPath)
        {
            RegistryKey key;
            string str = "";
            try
            {
                key = OpenRegistryPath(inPath, true);
            }
            catch
            {
                key = OpenRegistryPath(inPath, false);
            }
            if (key == null)
            {
                throw new Exception("Unable to access expiration date.");
            }
            string name = "id";
            if (key.GetValue(name) == null)
            {
                string str3 = Encode(Convert.ToString(DateTime.Now, CultureInfo.InvariantCulture.DateTimeFormat));
                key.SetValue(name, str3);
                Log.Success(Name + " registered successfully.");
            }
            object obj2 = key.GetValue(name);
            if (obj2 == null)
            {
                throw new Exception("Unable to find expiration date.");
            }
            str = Decode((string) obj2);
            key.Close();
            DateTime maxValue = DateTime.MaxValue;
            try
            {
                maxValue = Convert.ToDateTime(str, CultureInfo.InvariantCulture.DateTimeFormat);
            }
            catch
            {
                throw new Exception("Unable to establish expiration date.");
            }
            return maxValue;
        }

        private static string GetRegistryString(string inPath, string inKey)
        {
            string str = "";
            object obj2 = null;
            RegistryKey key = OpenRegistryPath(inPath, false);
            if (key != null)
            {
                obj2 = key.GetValue(inKey);
            }
            if (obj2 is string)
            {
                str = (string) obj2;
            }
            return str;
        }

        private static TimeSpan GetTimeLeft()
        {
            DateTime installDate = InstallDate;
            TimeSpan zero = TimeSpan.Zero;
            if (installDate == DateTime.MaxValue)
            {
                return zero;
            }
            zero = DateTime.Now.Subtract(installDate);
            if ((zero.TotalDays >= 0.0) && (zero.TotalDays <= 30.0))
            {
                TimeSpan span2 = new TimeSpan(30, 0, 0, 0);
                return span2.Subtract(zero);
            }
            return TimeSpan.Zero;
        }

        public static void Install()
        {
            string license = License;
        }

        internal static bool IsValid(string inString)
        {
            if (!IsValidPro(inString))
            {
                IsValidLite(inString);
            }
            return true;
        }

        internal static bool IsValidLite(string inString)
        {
            bool flag = true;
            try
            {
                if (inString.Length < 12)
                {
                    return false;
                }
                int num = Convert.ToInt32(inString.Substring(0, 12).Replace("-", ""));
                if ((((0x41c64e6d * num) + 0x3039) & 0x7fff) != 0x3bd7)
                {
                    return false;
                }
                if ((((0x3d41c64e * num) + 0x3a18) & 0x7fff) != 0x4a4c)
                {
                    return false;
                }
                if ((((0x2e3039c6 * num) + 0x41c6) & 0x7fff) != 0x244a)
                {
                    return false;
                }
                if ((((0x164e6d41 * num) + 0x44a7) & 0x7fff) != 0x577d)
                {
                    return false;
                }
                sLite = true;
                flag = true;
            }
            catch
            {
            }
            return flag;
        }

        internal static bool IsValidPro(string inString)
        {
            bool flag = true;
            try
            {
                if (inString.Length < 12)
                {
                    return false;
                }
                int num = Convert.ToInt32(inString.Substring(0, 12).Replace("-", ""));
                if ((((0x41c64e6d * num) + 0x3039) % 0x7fff) != 0x4291)
                {
                    return false;
                }
                if ((((0x3d41c64e * num) + 0x3a18) % 0x7fff) != 0x55b5)
                {
                    return false;
                }
                if ((((0x1e3039c6 * num) + 0x41c6) % 0x7fff) != 0x5fe7)
                {
                    return false;
                }
                if ((((0x164e6d41 * num) + 0x44a7) % 0x7fff) != 0x3359)
                {
                    return false;
                }
                sLite = false;
                flag = true;
            }
            catch
            {
            }
            return flag;
        }

        private static RegistryKey OpenRegistryPath(string inPath, bool inWritable)
        {
            string[] strArray = inPath.Split(new char[] { '\\' });
            RegistryKey key = null;
            for (int i = 0; i < strArray.Length; i++)
            {
                if (i == 0)
                {
                    try
                    {
                        key = Registry.LocalMachine.OpenSubKey(strArray[i], inWritable);
                    }
                    catch
                    {
                    }
                    if (key == null)
                    {
                        key = Registry.LocalMachine.OpenSubKey(strArray[i]);
                    }
                }
                else
                {
                    RegistryKey key2 = key.OpenSubKey(strArray[i], inWritable);
                    if (key2 == null)
                    {
                        key2 = key.CreateSubKey(strArray[i]);
                    }
                    key.Close();
                    key = key2;
                }
                if (key == null)
                {
                    return key;
                }
            }
            return key;
        }

        private static void SetRegistryString(string inPath, string inKey, string inValue)
        {
            RegistryKey key = OpenRegistryPath(inPath, true);
            if (key != null)
            {
                key.SetValue(inKey, inValue);
            }
        }

        public static void Uninstall()
        {
        }

        internal static DateTime InstallDate
        {
            get
            {
                DateTime maxValue = DateTime.MaxValue;
                try
                {
                    string str = Decode(HttpUtility.UrlDecode(Settings.Current.Advanced.Serial));
                    if (str != "")
                    {
                        maxValue = Convert.ToDateTime(str, CultureInfo.InvariantCulture.DateTimeFormat);
                    }
                }
                catch
                {
                }
                try
                {
                    if (maxValue == DateTime.MaxValue)
                    {
                        maxValue = GetInstallDate(Path);
                    }
                }
                catch
                {
                }
                return maxValue;
            }
        }

        internal static bool IsLite
        {
            get
            {
                if (!sChecked)
                {
                    IsValid(SerialKey);
                    sChecked = true;
                }
                return sLite;
            }
        }

        public static string License
        {
            get
            {
                try
                {
                    if (IsValidPro(SerialKey))
                    {
                        return string.Format("Full License ({0})", Serial);
                    }
                    if (IsValidLite(SerialKey))
                    {
                        return string.Format("Lite License ({0})", Serial);
                    }
                    TimeSpan timeLeft = GetTimeLeft();
                    if (timeLeft.TotalDays <= 0.0)
                    {
                        return "Trial License - Expired";
                    }
                    return string.Format("Trial License - {0:D2} days {1:D2}:{2:D2}:{3:D2} Left", new object[] { timeLeft.Days, timeLeft.Hours, timeLeft.Minutes, timeLeft.Seconds });
                }
                catch (Exception exception)
                {
                    return string.Format("No License. {0}", exception.Message);
                }
            }
        }

        private static string Name
        {
            get
            {
                if (sName == null)
                {
                    sName = Assembly.GetExecutingAssembly().GetName().Name;
                }
                return sName;
            }
        }

        private static string Path
        {
            get
            {
                if (sPath == null)
                {
                    sPath = @"SOFTWARE\WebSupergoo\" + Name + @"\" + Version;
                }
                return sPath;
            }
        }

        public static string Serial
        {
            get
            {
                string serialKey = SerialKey;
                if (serialKey.Length < 12)
                {
                    return "";
                }
                return serialKey.Substring(12, serialKey.Length - 12);
            }
            set
            {
                SerialKey = value;
            }
        }

        internal static string SerialKey
        {
            get
            {
                string registryString = "";
                try
                {
                    registryString = GetRegistryString(Path, "serial");
                }
                catch
                {
                }
                try
                {
                    if (registryString == "")
                    {
                        registryString = Settings.Current.Advanced.Serial;
                    }
                }
                catch
                {
                }
                return registryString;
            }
            set
            {
                if (IsValid(value))
                {
                    SetRegistryString(Path, "serial", value);
                }
            }
        }

        public static string TrialKey
        {
            get
            {
                try
                {
                    return HttpUtility.UrlEncode(Encode(Convert.ToString(GetInstallDate(Path), CultureInfo.InvariantCulture.DateTimeFormat)));
                }
                catch
                {
                    return null;
                }
            }
        }

        private static string Version
        {
            get
            {
                if (sVers == null)
                {
                    sVers = Assembly.GetExecutingAssembly().GetName().Version.Major.ToString();
                }
                return sVers;
            }
        }
    }
}

